package com.jnchen.jwt.entity;

import com.jnchen.jwt.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户对象
 *
 * @author jnchen
 * @date 2020/10/30 09:35
 */
public class User {

    /**
     * 用户id
     */
    private Integer id;

    /**
     * 用户名称
     */
    private String name;

    /**
     * 用户操作
     */
    private List<String> operations;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getOperations() {
        return operations;
    }

    public void setOperations(List<String> operations) {
        this.operations = operations;
    }

    /**
     * 生成一个随机的用户
     */
    public static User randomUser() {
        User user = new User();
        user.setId(RandomUtil.randomNumber(1000));
        user.setName(RandomUtil.randomStr());
        List<String> ops = new ArrayList<>();
        int len = RandomUtil.randomNumber(30);
        for (int i = 0; i < len; i++) {
            ops.add(RandomUtil.randomStr());
        }
        user.setOperations(ops);
        return user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        User user = (User) o;
        return Objects.equals(id, user.id) &&
                Objects.equals(name, user.name) &&
                listEquals(operations, user.getOperations());
    }

    private boolean listEquals(List<String> op1, List<String> op2) {
        if (op1.size() != op2.size()) {
            return false;
        }
        for (int i = 0; i < op1.size(); i++) {
            if (!op1.get(i).equals(op2.get(i))) {
                return false;
            }
        }
        return true;
    }

}
